package com.sinodata.bsm.cicp.finder.network;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.snmp4j.CommunityTarget;
import org.snmp4j.Snmp;

import com.sinodata.bsm.cicp.util.snmp.GetData;
import com.sinodata.bsm.cicp.util.snmp.MibInterfaceEntry;
import com.sinodata.bsm.cicp.util.snmp.OMMappingInfo;

/**
 * <p>
 * Description: 网络设备接口发现程序
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-9-3 下午3:50:15          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class InterfaceFinder extends NetWorkFinder {

    private static final Logger logger = Logger.getLogger(InterfaceFinder.class);

    @Override
    public List<String[]> find(Snmp snmp, CommunityTarget target) {

        List<String[]> resultList = new ArrayList<String[]>();
        try {
            List<OMMappingInfo> interfaceList = GetData.getAllTableData(snmp, MibInterfaceEntry.class, target);
            if (interfaceList == null || interfaceList.size() < 1) {
                throw new Exception("发现网络设备[" + ip + "]异常，程序使用的oid没有获取到相关数据");
            }
            for (OMMappingInfo mapping : interfaceList) {
                MibInterfaceEntry interfaceMapping = (MibInterfaceEntry) mapping;
                if (interfaceMapping.getIfIndex() == 0) {
                    continue;
                }
                String[] attr = new String[3 + getAttributes().length];
                resultList.add(attr);
                attr[0] = ip + "_" + interfaceMapping.getIfIndex();
                attr[1] = getRightResName(interfaceMapping.getIfDescr());
                attr[2] = ip + "_" + interfaceMapping.getIfDescr();
                attr[3] = interfaceMapping.getIfIndex() + "";
                attr[4] = "";//子网掩码
                attr[5] = interfaceMapping.getIfSpeed() + "";
                attr[6] = interfaceMapping.getIfSpeed() + "";
                attr[7] = interfaceMapping.getIfPhysAddress();
                attr[8] = "";//连接IP
                attr[9] = interfaceMapping.getIfAdminStatus() + "";
            }
        } catch (Exception e) {
            logger.error(ip + " 网络接口发现失败", e);
        }
        return resultList;
    }

    @Override
    public String[] getAttributes() {
        return new String[] { "端口索引号", "子网掩码", "入工作速率", "出工作速率", "mac地址", "连接IP", "管理状态" };
    }

    private String getRightResName(String resName) {
        if (resName == null || resName.length() < 40) {
            return resName;
        }
        if (resName.indexOf(" ") < 0) {
            return resName.substring(40);
        }
        String[] array = resName.split("\\s+");
        StringBuffer buffer = new StringBuffer();
        int i = 0;
        for (String s : array) {
            i += s.length();
            if (i < 40) {
                buffer.append(s);
                buffer.append(" ");
            } else {
                break;
            }
        }
        if (buffer.length() == 0) {
            buffer.append(resName.substring(40));
        }
        return buffer.toString().trim();
    }

}
